package com.gjy.nwpufindseats;
import java.util.Timer;
import java.util.TimerTask;
import com.gjy.nwpufindseats.Fragment.Fragment_home;
import com.gjy.nwpufindseats.Fragment.Fragment_room;
import com.gjy.nwpufindseats.baseData.Global;
import com.gjy.nwpufindseats.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends SlidingFragmentActivity implements OnClickListener{
	protected SlidingMenu mSlidingMenu;
	private ImageButton ivTitleBtnLeft;
	private Fragment mContent;
	private TextView aBtn;
	private TextView cBtn;
	private TextView dBtn;
	private TextView gBtn;
	Context mainContext;
	
	final int MENU_EXIT = Menu.FIRST+2;
	
	private static Boolean isExit =false;      
	private static Boolean hasTask =false;
	public static SharedPreferences preferences;
	public static Editor editor;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mainContext = MainActivity.this;
		initSlidingMenu();
		setContentView(R.layout.activity_main);
		aBtn = (TextView)findViewById(R.id.aBtn);
    	aBtn.setOnClickListener(this);
    	cBtn = (TextView) findViewById(R.id.cBtn);
    	cBtn.setOnClickListener(this);
    	dBtn =  (TextView) findViewById(R.id.dBtn);
    	dBtn.setOnClickListener(this);
    	gBtn = (TextView) findViewById(R.id.gBtn);
    	gBtn.setOnClickListener(this);
		initView();
	}
	
	
	private void initView() {
		ivTitleBtnLeft = (ImageButton)this.findViewById(R.id.ivTitleBtnLeft);
		ivTitleBtnLeft.setOnClickListener(this);
		
	}

	//--------------------初始化左右侧菜单
	private void initSlidingMenu() {
		mContent = new Fragment_home();
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, mContent)
		.commit();
		
		//设置左侧菜单的左视图界面
		setBehindContentView(R.layout.main_left_layout);//设置左边的菜单布局
		FragmentTransaction mFragementTransaction = getSupportFragmentManager()
				.beginTransaction();
		Fragment leftFrag = new LeftSlidingMenuFragment();
		mFragementTransaction.replace(R.id.main_left_fragment, leftFrag);
		mFragementTransaction.commit();
		mSlidingMenu = getSlidingMenu();
		mSlidingMenu.setMode(SlidingMenu.LEFT);// 设置是左滑
		mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// 设置菜单宽度
		mSlidingMenu.setFadeDegree(0.35f);// 设置淡入淡出的比例
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//设置手势模式
		mSlidingMenu.setShadowDrawable(R.drawable.shadow);// 设置左菜单阴影图片
		mSlidingMenu.setFadeEnabled(true);// 设置滑动时菜单的是否淡入淡出
		mSlidingMenu.setBehindScrollScale(0.333f);// 设置滑动时拖拽效果
		
	}

	@Override
	public void onClick(View v) {
		Fragment newContent = null;
		Bundle bundle = new Bundle();
		switch (v.getId()) {
			case R.id.ivTitleBtnLeft:
				//点击标题左边按钮弹出左侧菜单
				mSlidingMenu.showMenu(true);
				break;
			case R.id.aBtn: //教西A
				newContent = new Fragment_room();
				aBtn.setSelected(true);
				cBtn.setSelected(false);
				dBtn.setSelected(false);
				gBtn.setSelected(false);
				bundle.putString("num", Global.apartmentNum[0]);//传"31"
				bundle.putString("name", Global.apartmentName[0]);
				newContent.setArguments(bundle);
				break;
			case R.id.cBtn:// 教西B
				newContent = new Fragment_room();
				aBtn.setSelected(false);
				cBtn.setSelected(true);
				dBtn.setSelected(false);
				gBtn.setSelected(false);
				bundle.putString("num", Global.apartmentNum[1]);
				bundle.putString("name", Global.apartmentName[1]);
				newContent.setArguments(bundle);
				break;
			case R.id.dBtn://教西D
				newContent = new Fragment_room();
				aBtn.setSelected(false);
				cBtn.setSelected(false);
				dBtn.setSelected(true);
				gBtn.setSelected(false);
				bundle.putString("num", Global.apartmentNum[2]);
				bundle.putString("name", Global.apartmentName[2]);
				newContent.setArguments(bundle);
			    break;
			case R.id.gBtn://研究生西馆
				newContent = new Fragment_room();
				aBtn.setSelected(false);
				cBtn.setSelected(false);
				dBtn.setSelected(false);
				gBtn.setSelected(true);
				bundle.putString("num", Global.apartmentNum[3]);
				bundle.putString("name", Global.apartmentName[3]);
				newContent.setArguments(bundle);
			    break;	
			default:
				break;
		}
		if (newContent != null)
			switchAbcdContent(newContent);
	}
   
	public void switchContent(Fragment fragment) {
		mContent = fragment;
		getSupportFragmentManager()
		.beginTransaction()
		.replace(R.id.content_frame, fragment)
		.commit();
		getSlidingMenu().showContent();
	}

	 public void switchAbcdContent(Fragment fragment) {
         getSupportFragmentManager()
         .beginTransaction()
         .replace(R.id.content_frame, fragment)
         .commit();
	 }
	 
	 
	 //--------------Menu键的生成
	 @Override
		public boolean onCreateOptionsMenu(Menu menu)
		{
		 super.onCreateOptionsMenu(menu);
			menu.add(0, MENU_EXIT, 0, "退出").setIcon(R.drawable.exit);		
			return super.onCreateOptionsMenu(menu);
		}
	 

	 	//-------------菜单项被单击后的回调方法
		@Override		
		public boolean onOptionsItemSelected(MenuItem mi)
		{
			switch (mi.getItemId())
			{				
				case MENU_EXIT:
					exit();
					break;
			}
			return true;
		}

		
		//-------------双击返回键退出-----
		 Timer tExit =new Timer();
		 TimerTask task =new TimerTask() {
		  @Override          
		  public void run() {              
		   isExit =false;              
		   hasTask =true;          
		   }      
		  };
		  
		  //------------重写返回方法，实现退出功能----------
		 @Override
		 public boolean onKeyDown(int keyCode, KeyEvent event) {
		   // TODO Auto-generated method stub
		   if (keyCode == KeyEvent.KEYCODE_BACK) {
		    if(isExit ==false ) {
		     isExit =true; 
		     Toast.makeText(this, "再按一次退出软件", Toast.LENGTH_SHORT).show();
		     if(!hasTask) {
		      tExit.schedule(task, 2000);  
		     }
		    } else 
		    {
		     finish();  
	         exit();	
		    }
		   }                  
		  return false;          
		 }
		 
		 //退出程序
		 public void exit()
		 {
			 if(!Global.savePwdBoolean)
		     {
				 preferences=this.getSharedPreferences("user", Context.MODE_PRIVATE);
				 editor = preferences.edit();
				 editor.clear();
				 editor.commit();
		     }
			 System.exit(0);
		 }

}
